package com.qianlima.upload.domain.service;

import com.qianlima.upload.common.dto.FileDto;
import com.qianlima.upload.domain.repository.IFileRepository;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Objects;

/**
 * @description: 文件仓储支持
 * @author：allms
 * @date: 2023/3/22 15:24
 */
public class FileRepositorySupport {

    @Resource
    private IFileRepository fileRepository;

    public FileDto findByKey(String key) {
        return fileRepository.findFileByKey(key);
    }

    public boolean saveOrUpdateFile(FileDto fileDto) {
        FileDto byKey = findByKey(fileDto.getFileKey());
        if (Objects.nonNull(byKey)) {
            fileDto.setUpdateTime(new Date());
            return fileRepository.updateFile(byKey.getFileKey(), byKey.getShardIndex());
        }
        fileDto.setCreateTime(new Date());
        return fileRepository.saveFile(fileDto);
    }
}
